/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers;

import entities.Event;
import entities.Invitation;
import entities.User;
import java.util.ArrayList;
import java.util.Iterator;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Alberto, Alessandra
 */
@Stateless
public class InvitationManager {
    
    @EJB
    SearchManager sm;
    
    @EJB
    UserManager um;
    
    @PersistenceContext
    EntityManager em;
    
    public void invite (Event event, User user) {
        Invitation invitation = new Invitation(user, event);
        event.addInvitation(invitation);
        user.addInvitation(invitation);
        em.persist(invitation);
        em.merge(event);
    }
    
    public ArrayList<User> getInvitedUsers (Event event) {
        ArrayList<User> users = new ArrayList<>();
        Iterator<Invitation> iter = event.getInvitations().iterator();
        while (iter.hasNext()) {
            users.add(iter.next().getUser());
        }
        return users;
    }
    
    public ArrayList<Invitation> getInvitationList () {
        ArrayList<Invitation> invitations = new ArrayList<>();
        invitations.addAll(em.createNamedQuery("findByUser" , Invitation.class)
            .setParameter("Iuser", um.getLoggedUser())
            .getResultList());
        return invitations;
    }
    
    public ArrayList<Invitation> getNewInvitations () {
        ArrayList<Invitation> invitations = new ArrayList<>();
        invitations.addAll(em.createNamedQuery("findByUserNew" , Invitation.class)
            .setParameter("Iuser", um.getLoggedUser())
            .getResultList());
        return invitations;
    }
    
    public void visualize (ArrayList<Invitation> invitations) {
        Iterator<Invitation> iter = invitations.iterator();
        while(iter.hasNext()){
            Invitation invitation = iter.next();
            invitation.setVisualized(Boolean.TRUE);
            em.merge(invitation);
        }
        
    }
    
    public void decline (Invitation invitation) {
        invitation.getEvent().removeInvitation(invitation);
        invitation.getUser().removeInvitation(invitation);
        Invitation inv = em.createNamedQuery("findById" , Invitation.class)
            .setParameter("Iuser", um.getLoggedUser())
            .setParameter("Ievent", invitation.getEvent())
            .getSingleResult();
        em.remove(inv);
    }
    
    public void accept (Invitation invitation) {
        invitation.setReplied(Boolean.TRUE);
        em.merge(invitation);
        User u = um.getLoggedUser();
        invitation.getEvent().addParticipant(u);
        u.addEvent(invitation.getEvent());
        em.merge(u);
        em.merge(invitation.getEvent());
    }
    
    public ArrayList<User> searchForInvite (String username, Event event) {
        ArrayList<User> users = new ArrayList<User>();
        users.addAll(sm.search(username));
        Iterator<User> iter = users.iterator();
        while(iter.hasNext()){
            User u = iter.next();
            if (getInvitedUsers(event).contains(u))
                iter.remove();
            if (u.equals(um.getLoggedUser()))
                iter.remove();
        }
        return users;
    }
    
}
